package mall.repository;


import mall.entity.Product;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductDao extends BaseDao<Product> {

    public List<Product> selectList(Product product) {
        StringBuilder sb = new StringBuilder("select * from  product where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (product.getId() != null) {
            sb.append(" and id = ?");
            params.add(product.getId());
        }
        if (product.getCategoryId() != null) {
            sb.append(" and category_id =  ?");
            params.add(product.getCategoryId());
        }
        if (product.getName() != null) {
            sb.append(" and name like ?");
            params.add("%" + product.getName() + "%");
        }

        return executeQuery(sb.toString(), params.toArray());
    }

    @Override
    public Product rowToObject(ResultSet rs) throws Exception {

        Product product = new Product();
        product.setId(rs.getInt("id"));
        product.setCategoryId(rs.getInt("category_id"));
        product.setName(rs.getString("name"));
        product.setDescription(rs.getString("description"));
        product.setImage(rs.getString("image"));
        product.setPrice(rs.getBigDecimal("price"));
        return product;
    }

    public Product selectById(Integer productId) {
        return executeQuery("select * from product where id = ? ", productId).get(0);
    }

    public int updateById(Product product) {
        StringBuilder sql = new StringBuilder("UPDATE product SET ");
        List<Object> params = new ArrayList<>();

        // 动态构建SQL和参数
        if (product.getName() != null) {
            sql.append("name = ?, ");
            params.add(product.getName());
        }
        if (product.getCategoryId() != null) {
            sql.append("category_id = ?, ");
            params.add(product.getCategoryId());
        }
        if (product.getPrice() != null) {
            sql.append("price = ?, ");
            params.add(product.getPrice());
        }
        if (product.getDescription() != null) {
            sql.append("description = ?, ");
            params.add(product.getDescription());
        }
        if (product.getImage() != null) {
            sql.append("image = ?, ");
            params.add(product.getImage());
        }

        // 移除最后一个逗号和空格
        if (sql.toString().endsWith(", ")) {
            sql.delete(sql.length() - 2, sql.length());
        }

        // 添加WHERE条件
        sql.append(" WHERE id = ?");
        params.add(product.getId());

        // 执行更新
        return executeUpdate(sql.toString(), params.toArray());
    }

    public int insert(Product product) {
        String sql = "insert into `product`(name, category_id, price, description, image) values(?, ?, ?, ?, ?)";
        return executeUpdate(sql, product.getName(), product.getCategoryId(), product.getPrice(), product.getDescription(), product.getImage());
    }

    public int deleteById(int id) {
        String sql = "delete from product where id = ?";
        return executeUpdate(sql, id);
    }

    public Integer count(Product product) {
        StringBuilder sb = new StringBuilder("select count(*) from  product where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (product.getId() != null) {
            sb.append(" and id = ?");
            params.add(product.getId());
        }
        if (product.getCategoryId() != null) {
            sb.append(" and category_id =  ?");
            params.add(product.getCategoryId());
        }
        if (product.getName() != null) {
            sb.append(" and name like ?");
            params.add("%" + product.getName() + "%");
        }

        return executeQueryCount(sb.toString(), params.toArray());
    }

    public List<Product> selectList(Product product,  Integer offset,Integer size) {
        StringBuilder sb = new StringBuilder("select * from  product where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (product.getId() != null && product.getId() != 0) {
            sb.append(" and id = ?");
            params.add(product.getId());
        }
        if (product.getCategoryId() != null) {
            sb.append(" and category_id =  ?");
            params.add(product.getCategoryId());
        }
        if (product.getName() != null && !"".equals(product.getName())) {
            sb.append(" and name like ?");
            params.add("%" + product.getName() + "%");
        }

        sb.append(" limit ?, ?");
        params.add(offset);
        params.add(size);

        return executeQuery(sb.toString(), params.toArray());
    }
}
